package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.conf.DeviceHistReqVo;
import com.miplus.ccrm.core.controller.vo.conf.DeviceHistResVo;
import com.miplus.ccrm.core.controller.vo.conf.UserInfoResVo;
import com.miplus.ccrm.core.mapper.SysDeviceHistMapper;
import com.miplus.ccrm.core.pojo.SysDeviceHist;
import com.miplus.ccrm.core.service.ExportService;
import com.miplus.ccrm.core.service.crd.FiImportHistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
* @author pc
* @description 针对表【sys_audit_log】的数据库操作Service实现
* @createDate 2024-03-07 10:34:19
*/
@Slf4j
@Service
public class SysDeviceHistServiceImpl extends MPJBaseServiceImpl<SysDeviceHistMapper, SysDeviceHist>
    implements SysDeviceHistService {

    @Resource
    private ExportService exportService;
    @Resource
    private FiImportHistService fiImportHistService;
    private IPage<DeviceHistResVo> getDeviceHistList(DeviceHistReqVo paras, String usrid)
    {
        return selectJoinListPage(
                new Page<>(paras.getPageNum(),paras.getPageSize()),
                DeviceHistResVo.class,
                new MPJLambdaWrapper<SysDeviceHist>()
                        .eq(StringUtils.isNotEmpty(paras.getGrpid()),SysDeviceHist::getDept,paras.getGrpid())
                        .eq(StringUtils.isNotEmpty(paras.getAccount()),SysDeviceHist::getUsrid,paras.getAccount())
                        .eq(StringUtils.isNotEmpty(paras.getDevice()),SysDeviceHist::getDevice,paras.getDevice())
                        .ge(StringUtils.isNotEmpty(paras.getCrdt1()),SysDeviceHist::getStartdate,paras.getCrdt1())
                        .le(StringUtils.isNotEmpty(paras.getCrdt2()),SysDeviceHist::getStartdate,paras.getCrdt2())
                        .ge(StringUtils.isNotEmpty(paras.getEnddt1()),SysDeviceHist::getEnddate,paras.getEnddt1())
                        .le(StringUtils.isNotEmpty(paras.getEnddt2()),SysDeviceHist::getEnddate,paras.getEnddt2())

                        .apply(StringUtils.isNotEmpty(paras.getMon())," t.startdate < date_add(str_to_date('" + paras.getMon()+ "-01 00:00:00','%Y-%m-%d %H:%i:%s'),INTERVAL 1 MONTH)")
                        .apply(StringUtils.isNotEmpty(paras.getMon())," ifnull(t.enddate,now()) >= str_to_date('" +paras.getMon() + "-01 00:00:00','%Y-%m-%d %H:%i:%s')")
                        .selectAll(SysDeviceHist.class)
                        .selectAs("fun_getusername(t.usrid)",DeviceHistResVo::getName)
                        .selectAs(SysDeviceHist::getDept,DeviceHistResVo::getGrpid)
                        .selectAs("(select grpname from sys_grp where grpid = t.dept)",DeviceHistResVo::getGroups)
                        .orderByDesc(SysDeviceHist::getStartdate)
                );
    }
    @Override
    public CmdResVo expDeviceHist(JSysUser user, DeviceHistReqVo paras, HttpServletResponse response)
    {
        IPage<DeviceHistResVo> pages = getDeviceHistList(paras,user.getUsrid());
        ArrayList arr=new ArrayList();

        String exptempid= JPubUtils.getExpTempID(paras.getExpfilename(),"分机列表");

        String filename=JPubUtils.getExpFileName(
                paras.getExpfilename(),exptempid
        );
        pages.convert(u->{
            HashMap map=JPubUtils.toExportMap(u);
            if(map!=null) arr.add(map);
            return u;
        });
        CmdResVo resVo=exportService.ToExcel(
                response,
                arr,
                paras.getExpidlist(),
                paras.getExpnamelist(),
                filename,
                user.getUsrid()
        );

        int okcount=resVo.getCount();
        if(okcount>0){
            fiImportHistService.AddExportHist(
                    resVo.getKeyId(),
                    user.getUsrid(),
                    exptempid,
                    filename,
                    okcount
            );
        }
        return resVo;
    }
}




